package cn.ctodb.cms.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.cms.service.CmsImgService;
import cn.ctodb.cms.web.rest.util.HeaderUtil;
import cn.ctodb.cms.service.dto.CmsImgDTO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing CmsImg.
 */
@RestController
@RequestMapping("/api")
public class CmsImgResource {

    private final Logger log = LoggerFactory.getLogger(CmsImgResource.class);
        
    @Inject
    private CmsImgService cmsImgService;

    /**
     * POST  /cms-imgs : Create a new cmsImg.
     *
     * @param cmsImgDTO the cmsImgDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new cmsImgDTO, or with status 400 (Bad Request) if the cmsImg has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/cms-imgs")
    @Timed
    public ResponseEntity<CmsImgDTO> createCmsImg(@RequestBody CmsImgDTO cmsImgDTO) throws URISyntaxException {
        log.debug("REST request to save CmsImg : {}", cmsImgDTO);
        if (cmsImgDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("cmsImg", "idexists", "A new cmsImg cannot already have an ID")).body(null);
        }
        CmsImgDTO result = cmsImgService.save(cmsImgDTO);
        return ResponseEntity.created(new URI("/api/cms-imgs/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("cmsImg", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /cms-imgs : Updates an existing cmsImg.
     *
     * @param cmsImgDTO the cmsImgDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated cmsImgDTO,
     * or with status 400 (Bad Request) if the cmsImgDTO is not valid,
     * or with status 500 (Internal Server Error) if the cmsImgDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/cms-imgs")
    @Timed
    public ResponseEntity<CmsImgDTO> updateCmsImg(@RequestBody CmsImgDTO cmsImgDTO) throws URISyntaxException {
        log.debug("REST request to update CmsImg : {}", cmsImgDTO);
        if (cmsImgDTO.getId() == null) {
            return createCmsImg(cmsImgDTO);
        }
        CmsImgDTO result = cmsImgService.save(cmsImgDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("cmsImg", cmsImgDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /cms-imgs : get all the cmsImgs.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of cmsImgs in body
     */
    @GetMapping("/cms-imgs")
    @Timed
    public List<CmsImgDTO> getAllCmsImgs() {
        log.debug("REST request to get all CmsImgs");
        return cmsImgService.findAll();
    }

    /**
     * GET  /cms-imgs/:id : get the "id" cmsImg.
     *
     * @param id the id of the cmsImgDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the cmsImgDTO, or with status 404 (Not Found)
     */
    @GetMapping("/cms-imgs/{id}")
    @Timed
    public ResponseEntity<CmsImgDTO> getCmsImg(@PathVariable Long id) {
        log.debug("REST request to get CmsImg : {}", id);
        CmsImgDTO cmsImgDTO = cmsImgService.findOne(id);
        return Optional.ofNullable(cmsImgDTO)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /cms-imgs/:id : delete the "id" cmsImg.
     *
     * @param id the id of the cmsImgDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/cms-imgs/{id}")
    @Timed
    public ResponseEntity<Void> deleteCmsImg(@PathVariable Long id) {
        log.debug("REST request to delete CmsImg : {}", id);
        cmsImgService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("cmsImg", id.toString())).build();
    }

    /**
     * SEARCH  /_search/cms-imgs?query=:query : search for the cmsImg corresponding
     * to the query.
     *
     * @param query the query of the cmsImg search 
     * @return the result of the search
     */
    @GetMapping("/_search/cms-imgs")
    @Timed
    public List<CmsImgDTO> searchCmsImgs(@RequestParam String query) {
        log.debug("REST request to search CmsImgs for query {}", query);
        return cmsImgService.search(query);
    }


}
